<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row type="flex" justify="space-between" style="margin: 30px 0">
      <el-col :span="16">
        <el-form inline>
          <el-form-item label="文章分类">
            <el-select
              v-model="params.cate_id"
              size="small"
              placeholder="请选择分类"
            >
              <el-option
                v-for="item in cateList"
                :key="item.id"
                :label="item.cate_name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发布状态">
            <el-select
              size="small"
              placeholder="请选择状态"
              v-model="params.state"
            >
              <el-option label="已发布" value="已发布"></el-option>
              <el-option label="草稿" value="草稿"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini">筛选</el-button>
            <el-button size="mini" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" size="small" @click="pubDialogVisible = true">
          发表文章
          <!-- 发布文章 -->
          <el-dialog
            title="发布文章"
            :visible.sync="pubDialogVisible"
            fullscreen
            :before-close="beforeClose"
            destroy-on-close

          >
            <ArtPub @pubDialogColse='pubDialogColseFn'></ArtPub>
            <!-- <template #footer>
              <el-button type="primary">发布文章</el-button>
              <el-button type="primary">存为草稿</el-button>
            </template> -->
          </el-dialog>
        </el-button>
      </el-col>
    </el-row>
    <!--  -->
    <el-table :data="list" style="width: 100%" border stripe>
      <el-table-column label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" @click="getArtInfo(row.id)">
            {{ row.title }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类"> </el-table-column>
      <el-table-column label="发表时间">
        <template #default="{ row }">
          <!-- {{row}} -->
          {{ row.pub_data | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"> </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="danger" size="small" @click="delArtById(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <!--
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"
 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px"
      :current-page="params.pagenum"
      :page-sizes="[1, 2, 3, 4, 5]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 文章详情的对话框 -->
    <el-dialog title="文章预览" :visible="dialogVisible" width="80%">
      <ArtInfo :artInfo="artInfo"></ArtInfo>
      <span slot="footer">
        <el-button type="primary" @click="dialogVisible = false"
          >关 闭</el-button
        >
      </span>
    </el-dialog>

    <!--  -->
  </el-card>
</template>

<script>
import { reqGetArtList, reqDelArtById, reqGetArtInfoById } from '@/api/article'
import { mapState, mapActions } from 'vuex'
import ArtInfo from '@/views/ArtList/components/ArtInfo.vue'
import ArtPub from '@/views/ArtList/components/ArtPub.vue'
export default {
  components: {
    ArtInfo,
    ArtPub
  },
  name: 'ArtList',
  data () {
    return {
      params: {
        pagenum: 1,
        pagesize: 5,
        cate_id: '',
        state: ''
      },
      total: 0,
      list: [],
      dialogVisible: false,

      // 文章详情对象
      artInfo: {},
      // 发布文章对话框
      pubDialogVisible: false
    }
  },
  created () {
    this.getArtList()
    this.getCateList()
  },
  methods: {
    ...mapActions('article', ['getCateList']),
    async getArtList () {
      const { data } = await reqGetArtList(this.params)
      console.log(data, 60)
      if (data.code !== 0) return this.$message.error(data.message)
      this.total = data.total
      this.list = data.data
    },
    // 重置
    reset () {
      this.params.cate_id = ''
      this.params.state = ''
      this.getArtList()
    },
    // 分页组件的渲染
    handleSizeChange (pagesize) {
      console.log(pagesize)
      this.params.pagesize = pagesize
      this.getArtList()
    },
    handleCurrentChange (pagenum) {
      console.log(pagenum)
      this.params.pagenum = pagenum
      this.getArtList()
    },
    // 删除
    delArtById (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文章,继续么?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          const { data } = await reqDelArtById(id)
          console.log(data, 130)
          if (data.code !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          // 如果是当前页最后一条数据, 删除以后回到上一页!!
          if (this.list.length === 1 && this.params.pagenum > 1) {
            this.params.pagenum--
          }
          this.getArtList()
        })
        .catch(() => {
          this.$message.info('取消删除')
        })
    },
    //
    async getArtInfo (id) {
      const { data } = await reqGetArtInfoById(id)
      console.log(data, 177)
      if (data.code !== 0) return this.$message.error(data.message)
      this.artInfo = data.data
      this.dialogVisible = true
    },
    //
    beforeClose (done) {
      this.$confirm('此操作将退出发布,是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          done()
        })
        .catch(() => {
          this.$message.info('已取消操作')
        })
    },
    // 发布完成-关闭对话框
    pubDialogColseFn () {
      this.pubDialogVisible = false
      // 渲染数据
      this.getArtList()
      // 销毁之前的数据 el-dialog属性  destroy-on-close
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-form-item__label {
  font-size: 12px;
}
</style>
